//
//  EQCardView.swift
//  EQQuestionBank
//
//  Created by Macx on 2018/8/5.
//  Copyright © 2018年 FanWeiJian. All rights reserved.
//

import UIKit
import SnapKit
import RxSwift

class EQCardView: UIView {
    
    private var model : EQQuestionModel!
        
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.layer.masksToBounds = true
        self.layer.cornerRadius = 5.0
        self.backgroundColor = .white
        setupSubViews()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setCardData(mode: EQQuestionModel){
        self.model = mode
        webView.loadHTMLString(mode.content, baseURL: nil)
    }
    
    func setupSubViews(){
        let bg = UIImageView(image: #imageLiteral(resourceName: "header_BG"))
        self.addSubview(bg)
        bg.snp.makeConstraints { (make) in
            make.top.left.right.equalToSuperview()
            make.height.equalTo(50)
        }
        
        headerView.snp.makeConstraints { (make) in
            make.top.left.right.equalToSuperview()
            make.height.equalTo(50)
        }
        
        let label = UILabel()
        label.text = "相关题库"
        label.textColor = UIColor.eq.hex(0x5d99f0)
        headerView.addSubview(label)
        label.snp.makeConstraints { (make) in
            make.center.equalToSuperview()
        }
        
        contentView.snp.makeConstraints { (make) in
            make.top.equalTo(headerView.snp.bottom)
            make.left.right.equalToSuperview()
            make.height.equalTo(227)
        }
        
        webView.snp.makeConstraints { (make) in
            make.top.equalTo(20)
            make.left.equalTo(20)
            make.right.equalTo(-20)
            make.bottom.equalTo(answerButton.snp.top).offset(-20)
        }
        
        
        answerButton.snp.makeConstraints { (make) in
            make.left.equalTo(20)
            make.height.equalTo(35)
            make.width.equalTo(100)
            make.bottom.equalTo(-20)
        }
        
        nextButton.snp.makeConstraints { (make) in
            make.right.equalTo(-20)
            make.height.equalTo(35)
            make.width.equalTo(100)
            make.bottom.equalTo(-20)
        }
        answerButton.rx.controlEvent(.touchUpInside)
            .subscribe {[weak self] (_) in
                NotificationCenter.post(customeNotification: EQNotification.pushAnalysisVC, object: self!.model)
            }.disposed(by: rx.disposeBag)
        
    }
    
    
    lazy var headerView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.eq.hex(0xc7ddfa)
        self.addSubview(view)
        return view
    }()
    
    lazy var contentView: UIView = {
        let view = UIView()
        view.backgroundColor = .white
        self.addSubview(view)
        return view
    }()
    
    lazy var answerButton: UIButton = {
        let button = UIButton()
        button.setTitle("查看答案", for: .normal)
        button.setTitleColor(.white, for: .normal)
        button.backgroundColor = UIColor.eq.hex(0x468dee)
        button.layer.cornerRadius = 5.0
        button.titleLabel?.font = UIFont.systemFont(ofSize: 13)
        contentView.addSubview(button)
        return button
    }()
    
    lazy var nextButton: UIButton = {
        let button = UIButton()
        button.setTitle("下一题", for: .normal)
        button.setTitleColor(.white, for: .normal)
        button.backgroundColor = UIColor.eq.hex(0x468dee)
        button.layer.cornerRadius = 5.0
        button.titleLabel?.font = UIFont.systemFont(ofSize: 13)
        contentView.addSubview(button)
        return button
    }()
    
    
    lazy var webView : UIWebView = {
        let wb = UIWebView()
        wb.scrollView.bounces = false
        contentView.addSubview(wb)
        return wb
    }()
}
